﻿using Entity;
using IRealize;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;

namespace Realize
{
    public class CFTK : ICFTK
    {
        public async Task<int> AddCFTKAsync(Config_File_Third_Kind cftk, string sqlserver)
        {
            using (SqlConnection sqlConnection = new SqlConnection(sqlserver))
            {
                string sql = $@"insert into [dbo].[config_file_third_kind]
              ([first_kind_id], [first_kind_name], [second_kind_id], [second_kind_name], [third_kind_id], [third_kind_name], [third_kind_sale_id], [third_kind_is_retail])
               values('{cftk.first_kind_id}','{cftk.first_kind_name}','{cftk.second_kind_id}','{cftk.second_kind_name}','{cftk.third_kind_id}','{cftk.third_kind_name}','{cftk.third_kind_sale_id}','{cftk.third_kind_is_retail}')";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }

        public async Task<int> DeleteCFTKAsync(int fsk_id, string sqlserver)
        {
            using (SqlConnection sqlConnection = new SqlConnection(sqlserver))
            {

                string sql = $@"delete from [dbo].[config_file_third_kind] where [ftk_id]='{fsk_id}'";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }

        public async Task<List<Config_File_Third_Kind>> SelectCFTKAsync(string sqlserver)
        {
            Task<List<Config_File_Third_Kind>> task = Task<List<Config_File_Third_Kind>>.Run(() =>
            {
                using (SqlConnection sqlConnection = new SqlConnection(sqlserver))
                {

                    string sql = $@"select * from [dbo].[config_file_third_kind]";

                    return sqlConnection.Query<Config_File_Third_Kind>(sql).ToList();
                }

            });

            return await task;
        }

        public async Task<Config_File_Third_Kind?> SelectCFTKIDAsync(int fsk_id, string sqlserver)
        {
            Task<Config_File_Third_Kind?> task = Task<Config_File_Third_Kind>.Run(() =>
            {
                using (SqlConnection sqlConnection = new SqlConnection(sqlserver))
                {

                    string sql = $@" select * from [dbo].[config_file_third_kind] where [ftk_id]='{fsk_id}'";

                    return sqlConnection.Query<Config_File_Third_Kind>(sql).FirstOrDefault();
                }

            });

            return await task;
        }

      

        public async Task<int> UpdateCFTKAsync(Config_File_Third_Kind cftk, string sqlserver, int id)
        {
            using (SqlConnection sqlConnection = new SqlConnection(sqlserver))
            {

                string sql = $@"update [dbo].[config_file_third_kind] set  [first_kind_name]='{cftk.first_kind_name}', [second_kind_id]='{cftk.second_kind_id}', [second_kind_name]='{cftk.second_kind_name}',  [third_kind_name]='{cftk.third_kind_name}', [third_kind_sale_id]='{cftk.third_kind_sale_id}', [third_kind_is_retail]='{cftk.third_kind_is_retail}' where [ftk_id] ='{id}' ";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }
    }
}
